#include "3DEDefine.h"
#include "GLTextureManager.h"
#include "StringUtil.h"
#include "GLTextureFileLoader.h"

/*! GLTextureManager::LoadTexture
* @param    ppTexture
* @param    name
* @param    path
* [puos] [9-20-10]
* @return   void
* @see
*/

CResourceData* GLTextureManager::CreateResource(char* name,char* path,ResourceHandle hResource,ResourceType tResource)
{
	//*
	string _str = CStringUtil::ToLower(name);

	GLTexture* pTexture = new GLTexture(this,hResource,const_cast<char*> (_str.c_str()),path);

    string _path = string(path);
	string _name = string(_str.c_str());

	string texturename = _path + _name;

	char* stexturename = const_cast<char*>(texturename.c_str());

    CGLTextureFileLoader TextureLoader;
	TextureLoader.Load(pTexture);

    return pTexture;
}

/*! GLTextureManager::BuildColorTexture
* @param    pTexture
* @param    r
* @param    g
* @param    b
* [puos] [9-20-10]
* @return   void
* @see
*/

void GLTextureManager::BuildColorTexture(GLTexture* pTexture,unsigned char r, unsigned char g, unsigned char b)
{
	unsigned char data[12];	// a 2x2 texture at 24 bits

	// Store the data
	for(int i = 0; i < 12; i += 3)
	{
		data[i] = r;
		data[i+1] = g;
		data[i+2] = b;
	}

	// Generate the OpenGL texture id
	glGenTextures(1, &pTexture->texture[0]);

	// Bind this texture to its id
	glBindTexture(GL_TEXTURE_2D, pTexture->texture[0]);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Use mipmapping filter
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	// Generate the texture
	//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 2, 2, GL_RGB, GL_UNSIGNED_BYTE, data);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 
}

